<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Attention;
use app\common\model\Dynamic as DynamicModel;
use app\common\model\DynamicComment;
use app\common\model\DynamicCommentReply;
use app\common\model\DynamicLike;
use app\common\model\RoomUser;


/**
 * 动态接口
 */
class Dynamic extends Api
{
    //protected $noNeedLogin = ['dynamic','rank'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     *家族动态列表
     */
    public function dynamicList()
    {
        $roomId = request()->post('room_id');
        $userId = request()->post('user_id');
        $follow = request()->post('follow');
        $page = request()->post('page',1);
        $limit = request()->post('limit',config('paginate.list_rows'));

        if(empty($roomId) && empty($userId) && empty($follow)){
            // 获取家族id
            $roomId = RoomUser::where('user_id',$this->auth->id)->where('types',2)->value('room_id');
        }

        if(!empty($roomId)){
            // 查询家族动态
            $userIds = RoomUser::where('room_id',$roomId)->where('status',1)->column('user_id');
        }
        if(!empty($follow)){
            // 查询我关注人的动态
            $userIds = Attention::where('user_id',$this->auth->id)->column('att_user_id');
        }
        if(!empty($userId)){
            // 查询用户动态
            $userIds = $userId;
        }

        if(!isset($userIds)){
            $userIds[] = $this->auth->id;
        }

        $list = DynamicModel::with('user,comment,comment.user')->where('user_id','IN',$userIds)->order('created_at','desc')->page($page,$limit)->select();
        foreach ($list as $v){
            $att = Attention::where('att_user_id',$v['user_id'])->where('user_id',$this->auth->id)->count();
            if($att >= 1){
                $v->is_attention = 1;
            }else{
                $v->is_attention = 0;
            }
            $isLike = DynamicLike::where('user_id',$this->auth->id)->where('dynamic_id',$v['id'])->count();
            if($isLike >= 1){
                $v->is_like = 1;
            }else{
                $v->is_like = 0;
            }
        }
        return $this->success('请求成功',$list);
    }


    /**
     *关注用户
     */

    public function attention()
    {
        $user_id = $this->auth->id;
        $att_user_id = $this->request->post('att_user_id');
        $att = Attention::where('att_user_id',$att_user_id)->where('user_id',$user_id)->count();
        if($att > 0){
            return $this->error('您已关注');
        }
        if($user_id == $att_user_id){
            return $this->error('不能关注自己');
        }
        Attention::create(['att_user_id'=>$att_user_id,'user_id'=>$user_id]);
        return $this->success('关注成功');
    }

    /**
     * 动态点赞
     */
    public function dynamicLike()
    {
        $user_id = $this->auth->id;
        $dynamic_id = $this->request->post('dynamic_id');
        $att = DynamicLike::where('user_id',$user_id)->where('dynamic_id',$dynamic_id)->count();
        if($att > 0){
            return $this->error('您已点过赞');
        }
        DynamicLike::create(['user_id'=>$user_id,'dynamic_id'=>$dynamic_id]);
        DynamicModel::where('id',$dynamic_id)->setInc('like_num',1);
        return $this->success('请求成功');
    }

    /**
     * 动态详情
     */
    public function dynamicDetail()
    {
        $dynamic_id = request()->post('dynamic_id');

        $list = DynamicModel::with('user')->where('id',$dynamic_id)->find();
        $att = Attention::where('att_user_id',$list['user_id'])->where('user_id',$this->auth->id)->count();
        if($att >= 1){
            $list->is_attention = 1;
        }else{
            $list->is_attention = 0;
        }
        DynamicModel::where('id',$dynamic_id)->setInc('look_num',1);
        return $this->success('请求成功',$list);
    }

    /**
     *发布动态
     */

    public function add(){
        $post = request()->post();
        $validate = new \think\Validate([
            'content'=> 'require',
            'images'  => 'require',
        ],[],[

            'content'  => '内容',
            'images'    => '图片',

        ]);
        if(!$validate->check($post)){
            $this->error($validate->getError());
        }
        $post['user_id'] = $this->auth->id;
        DynamicModel::create($post);
        return $this->success('发布成功');
    }

    /**
     * 删除动态
     */
    public function del(){
        $dynamic_id = $this->request->post('dynamic_id');
        if(empty($dynamic_id)){
            $this->error('参数错误');
        }
        $count = DynamicModel::where('id',$dynamic_id)->where('user_id',$this->auth->id)->count();
        if($count < 1){
            $this->error('权限不足,无法操作');
        }
        DynamicModel::destroy($dynamic_id);
        $this->success('删除成功');
    }

    /**
     * 添加评论
     */
    public function addComment(){
        $post = request()->post();
        if(!isset($post['content']) || empty($post['content'])){
            $this->error('参数错误');
        }
        if(isset($post['c_id']) && !empty($post['c_id'])){
            // 回复
            $commentCount = DynamicComment::where('id',$post['c_id'])->count();
            if($commentCount < 1){
                $this->error('参数错误!');
            }
            $data = [
                'c_id'   => $post['c_id'],
                'content'=> $post['content'],
                'user_id'=> $this->auth->id,
            ];
            if(isset($post['reply_id']) && !empty($post['reply_id'])){
                $reply = DynamicCommentReply::where('id',$post['reply_id'])->field('id,user_id')->find();
                if($reply['user_id'] == $this->auth->id){
                    $this->error('无法回复自己');
                }
                if(!empty($reply)){
                    $data['to_user_id'] = $reply['user_id'];
                    $data['reply_id'] = $post['reply_id'];
                }
            }
            $result = DynamicCommentReply::create($data);
            if($result){
                $this->success('回复成功');
            }
            $this->error('回复失败');
        }else{
            // 评论
            if(!isset($post['d_id']) || empty($post['d_id'])){
                $this->error('参数错误');
            }
            $feedCount = DynamicModel::where('id',$post['d_id'])->where('status',1)->count();
            if($feedCount < 1){
                $this->error('参数错误!');
            }
            $result = DynamicComment::create([
                'd_id'      => $post['d_id'],
                'user_id'   => $this->auth->id,
                'content'   => $post['content']
            ]);
            if($result){
                $this->success('评论成功');
            }
            $this->error('评论失败');
        }
    }

    /**
     * 评论列表
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function commentList(){
        $dId = request()->post('d_id');
        $page = request()->post('page',1);
        $limit = request()->post('limit',config('paginate.list_ros'));
        $feedCount = DynamicModel::where('id',$dId)->where('status',1)->count();
        if($feedCount < 1){
            $this->error('参数错误');
        }
        $list = DynamicComment::with(['user','reply'=>function($query){
            return $query->with('user,toUser');
        }])->where('d_id',$dId)->order('created_at','asc')->page($page,$limit)->select();
        $this->success('请求成功',$list);
    }
}
